import { reactive, computed } from 'vue'
import routerInstance from '@/router/index.js'

/**
 * 控制左侧菜单展示状态
 */
export const leftMenuStore = reactive({
    isHideLeftMenu: true,
    isShowLeftMenu: computed(() => { return !(leftMenuStore.isHideLeftMenu) }),
    hideLeftMenu() {
        this.isHideLeftMenu = true
    },
    showLeftMenu() {
        this.isHideLeftMenu = false
    },
})

/**
 * 全局loading
 */
export const globalLoading = reactive({
    loading: false,
    openGlobalLoading() {
        this.loading = true
    },
    closeGlobalLoading() {
        this.loading = false
    },
})

export const loginState = reactive({
    isLogin: false,
    login() {
        this.isLogin = true
    },
    noLogin() {
        this.isLogin = false
    }
})

import { searchMenuTreeAPI } from '@/api/rbac/menu.js'

export const systemRouterStore = reactive({
    routers: null,
    setRouters(routerVO) {
        this.routers = routerVO
        pushRouter(routerVO.list)
    },
    getRouters() {
        return this.routers
    },
    getRouterList() {
        return this.routers ? this.routers.list : null
    },
    getRouterTree() {
        return this.routers ? this.routers.tree : null
    },
    async reloadRouter() {
        let routers = await searchMenuTreeAPI({ systemId: 3 });
        if(routers) {
            this.setRouters(routers.data.data) //保存路由信息
        }
    },
    remove() {
        this.routers = null
    }
})

const modules = import.meta.glob("@/views/**/*.vue")

export function pushRouter(routers) {
    routers.forEach(systemRouter => {
        if(systemRouter.type == 1 || systemRouter.type == 2) {    //页面 or 组件类型
            let componentPath = systemRouter.componentPath
            if(componentPath && systemRouter.componentPath.charAt(0) == '@') {
                componentPath = '/src' + systemRouter.componentPath.substr(1)
            }
            let component = modules[/* @vite-ignore */ `${componentPath}`]
            routerInstance.addRoute({ 
                path: systemRouter.path, 
                name: systemRouter.code,
                component,
                meta: { title: systemRouter.title ? systemRouter.title : '' }
            })
        }
    })
}